package com.abc.xml.dom;

import com.abc.xml.sax.User;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class DomUtil {

   public void insert(User user) {
      try {
         // 获取工厂对象
         DocumentBuilderFactory bbf = DocumentBuilderFactory.newInstance();
         // 获取构建对象
         DocumentBuilder builder = bbf.newDocumentBuilder();
         // 解析路径得到文档对象
         Document doc = builder.parse(getInputStream());
         // 获取根元素
         Element root = doc.getDocumentElement();
         // 创建linkman元素
         Element linkmanEl = doc.createElement("user");
         linkmanEl.setAttribute("id", user.getId());
         // 把linkman作为root的子节点
         root.appendChild(linkmanEl);
         // 创建name、address、email
         Element nameEl = doc.createElement("name");
         Element addressEl = doc.createElement("password");
         // 给他们赋值内容
         nameEl.setTextContent(user.getName());
         addressEl.setTextContent(user.getPassword());
         // 建立name、address、email和linkman标签的关系
         linkmanEl.appendChild(nameEl);
         linkmanEl.appendChild(addressEl);

         // 同步 将内存中的xml树,写到文件中
         TransformerFactory factory = TransformerFactory.newInstance();
         Transformer transformer = factory.newTransformer();
         transformer.transform(new DOMSource(doc), new StreamResult("resources/users.xml"));
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }

   public void update(User user) {
      try {
         // 获取工厂对象
         DocumentBuilderFactory bbf = DocumentBuilderFactory.newInstance();
         // 获取构建对象
         DocumentBuilder builder = bbf.newDocumentBuilder();
         // 解析路径得到文档对象
         Document doc = builder.parse(getInputStream());
         // 获取根元素
         Element root = doc.getDocumentElement();
         // 获取所有的Linkman元素
         NodeList nodeList = root.getElementsByTagName("user");
         for (int i = 0; i < nodeList.getLength(); i++) {
            // 获取linkman元素的id属性值,来判断
            Element linkmanEl = (Element) nodeList.item(i);
            String linkmanId = linkmanEl.getAttribute("id");
            if (linkmanId.equals(user.getId())) {
               linkmanEl.getElementsByTagName("name").item(0).setTextContent(user.getName());
               linkmanEl.getElementsByTagName("password").item(0).setTextContent(user.getPassword());
               //同步
               TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc),
                       new StreamResult("resources/users.xml"));
               return;
            }
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }

   public void delete(String id) {
      try {
         // 获取工厂对象
         DocumentBuilderFactory bbf = DocumentBuilderFactory.newInstance();
         // 获取构建对象
         DocumentBuilder builder = bbf.newDocumentBuilder();
         // 解析路径得到文档对象
         Document doc = builder.parse(getInputStream());
         // 获取根元素
         Element root = doc.getDocumentElement();
         // 获取所有的Linkman元素
         NodeList nodeList = root.getElementsByTagName("user");
         for (int i = 0; i < nodeList.getLength(); i++) {
            // 获取linkman元素的id属性值,来判断
            Element linkmanEl = (Element) nodeList.item(i);
            String linkmanId = linkmanEl.getAttribute("id");
            if (linkmanId.equals(id)) {
               // 删除
               root.removeChild(linkmanEl);
               // 同步
               TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc),
                       new StreamResult("resources/users.xml"));
               return;
            }
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }

   public User selectOneById(String id) {
      try {
         // 获取工厂对象
         DocumentBuilderFactory bbf = DocumentBuilderFactory.newInstance();
         // 获取构建对象
         DocumentBuilder builder = bbf.newDocumentBuilder();
         // 解析路径得到文档对象
         Document doc = builder.parse(getInputStream());
         // 获取根元素
         Element root = doc.getDocumentElement();
         // 获取所有的Linkman元素
         NodeList nodeList = root.getElementsByTagName("user");
         for (int i = 0; i < nodeList.getLength(); i++) {
            // 获取linkman元素的id属性值,来判断
            Element linkmanEl = (Element) nodeList.item(i);
            String linkmanId = linkmanEl.getAttribute("id");
            if (linkmanId.equals(id)) {
               // 如果等于参数id,就去获取对应的name、address、email
               String name = linkmanEl.getElementsByTagName("name").item(0).getTextContent();
               String address = linkmanEl.getElementsByTagName("password").item(0).getTextContent();
               // 创建Linkman对象
               User user = new User();
               user.setId(id);
               user.setName(name);
               user.setPassword(address);
               return user;
            }
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }

   public List<User> selectAll() {
      List<User> list = new ArrayList<>();
      // Dom方法
      // 获取Document对象
      // 1.获取DocumentBuilderFactory对象
      DocumentBuilderFactory bbf = DocumentBuilderFactory.newInstance();
      // 2.使用DocumentBuilderFactory创建DocumentBuilder对象
      try {
         DocumentBuilder builder = bbf.newDocumentBuilder();
         // 3.使用DocumentBuilder解析一个xml路径，得到一个Document对象
         Document doc = builder.parse(getInputStream());
         // 获取根元素
         Element root = doc.getDocumentElement();
         // 获取所有的linkman
         NodeList linkmans = root.getElementsByTagName("user");
         for (int i = 0; i < linkmans.getLength(); i++) {
            // 遍历得到每一个linkman标签
            Element linkmanEl = (Element) linkmans.item(i);
            // 通过linkman标签 去获取name标签、address标签、email标签
            Element nameEl = (Element) linkmanEl.getElementsByTagName("name").item(0);
            Element addressEl = (Element) linkmanEl.getElementsByTagName("password").item(0);
            // 获取name、address、email的值
            String name = nameEl.getTextContent();
            String address = addressEl.getTextContent();
            // 获取linkman标签的id属性
            String id = linkmanEl.getAttribute("id");

            // 创建Linkman对象
            User user = new User();
            user.setId(id);
            user.setName(name);
            user.setPassword(address);
            // 将linkman对象添加到集合
            list.add(user);
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      return list;
   }

   public InputStream getInputStream(){
      return  DomUtil.class.getClassLoader().getResourceAsStream("users.xml");
   }

}
